//
//  LcbApiService.m
//  lcb
//
//  Created by lcb on 2017/1/23.
//  Copyright © 2017年 liuchengbin. All rights reserved.
//

#import "LcbApiService.h"
#import "LcbNetworking.h"
#import "LcbJson.h"
#import "LcbBaseModel.h"
@implementation LcbApiService
/**
 *  配置请求头
 *
 *  @param httpHeader 请求头
 */
+ (void)configHttpHeader:(NSDictionary *)httpHeader
{
    [LcbNetworking configHttpHeader:httpHeader];
}

/**
 *  取消GET请求
 */
+ (void)cancelRequestWithURL:(NSString *)url
{
    [LcbNetworking cancelRequestWithURL:url];
}

/**
 *  取消所有请求
 */
+ (void)cancleAllRequest
{
    [LcbNetworking cancleAllRequest];
}

/**
 * json格式的post请求方法
 * @param param 请求参数
 * @param clazz response对象的类型
 * @param loadingDelegate 上传进度反馈
 * @param loadCache 加载缓存的反馈
 * @param success 上传成功反馈
 * @param fail 上传失败反馈
 */
+(void)requestWithParam:(LcbRequestParam*)param
          progressBlock:(RequestProgressBlock)progressBlock
                success:(RequestSuccessBlock)successHandler
                failure:(RequestFailBlock)failureHandler
{
    return [self requestWithParam:param refreshRequest:NO cache:NO progressBlock:progressBlock success:successHandler failure:failureHandler];
}

//refresh:当遇到重复请求时，若为YES，则会取消旧的请求，用新的请求，若为NO，则忽略新请求，用旧请求
+(void)requestWithParam:(LcbRequestParam*)param
         refreshRequest:(BOOL)refresh
                  cache:(BOOL)cache
          progressBlock:(RequestProgressBlock)progressBlock
          success:(RequestSuccessBlock)successHandler
          failure:(RequestFailBlock)failureHandler
{
    //配置请求头
    [self configHttpHeader:param.headers];
    //
    NSLog(@"-----------%@------%@",param.url,[param getBodys]);
    [LcbNetworking requestWithUrl:param.url method:param.requestModel refreshRequest:refresh cache:cache params:[param getBodys] progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        !progressBlock?:progressBlock(bytesRead,totalBytes);
    } successBlock:^(id response) {
        if ([NSJSONSerialization isValidJSONObject:response]) {
            NSDictionary *res=(NSDictionary*)response;
            NSLog(@"-----------\n%@",res);
             id object =[LcbJson jsonToObject:response withClass:[LcbBaseModel class]];
            !successHandler?:successHandler(object,res[@"message"]);
        }else
        {
            !failureHandler?:failureHandler(@"接口异常");
            
        }
    } failBlock:^(NSError *error) {
        !failureHandler?:failureHandler(error.localizedDescription);
    }];
}
/**
 *  多文件上传
 *
 *  @param url           上传文件地址
 *  @param params           拼接参数
 *  @param datas         数据集合
 *  @param progressBlock 上传进度
 *  @param successBlock  成功回调
 *  @param failBlock     失败回调
 *
 *  @return 任务集合
 */
+ (void)uploadMultFileWithParams:(LcbRequestParam*)param
                         fileDatas:(NSArray *)datas
                      successBlock:(RequestSuccessBlock)successBlock
                         failBlock:(RequestFailBlock)failBlock
{
    //配置请求头
    [self configHttpHeader:param.headers];
    [LcbNetworking uploadFileWithUrl:param.url params:[param getBodys] fileDatas:datas progressBlock:nil successBlock:^(id response) {
        if ([NSJSONSerialization isValidJSONObject:response]) {
            NSDictionary *res=(NSDictionary*)response;
            id object =[LcbJson jsonToObject:response withClass:[LcbBaseModel class]];
            !successBlock?:successBlock(object,res[@"message"]);
        }else
        {
            !failBlock?:failBlock(@"接口异常");
            
        }
    } failBlock:^(NSError *error) {
        !failBlock?:failBlock(error.localizedDescription);
    }];
}
/**
 *  文件下载
 *
 *  @param params           拼接参数
 *  @param progressBlock 下载进度
 *  @param successBlock  成功回调
 *  @param failBlock     下载回调
 *
 *  @return 返回的对象可取消请求
 */
+ (void)downloadWithParams:(LcbRequestParam*)param
             progressBlock:(RequestProgressBlock)progressBlock
              successBlock:(RequestSuccessBlock)successBlock
                 failBlock:(RequestFailBlock)failBlock
{
    if (param.downloadurl==nil ||param.downloadurl.length==0) {
        !failBlock?:failBlock(@"接口异常");
    }
    //配置请求头
    [self configHttpHeader:param.headers];
    [LcbNetworking downloadWithUrl:param.downloadurl progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        !progressBlock?:progressBlock(bytesRead,totalBytes);
    } successBlock:^(NSURL *url) {
        !successBlock?:successBlock(url,@"");
    } failBlock:^(NSError *error) {
        !failBlock?:failBlock(error.localizedDescription);
    }];
}
@end
